<?php
class chapter_language extends Model{
	public static function getTableName(){
		return 'chapter-language';
	}

    public function getPrimaryKey()
    {
        return array('chapter_id', 'language_id');
    }

	public function set($object){
		$this->title= !isset($object->title) ? '' : trim($object->title);
		$this->description= !isset($object->description) ? '' : $object->description;
	}

	public function getLangChapterByID($chapter_id, $lan_id)
	{
		$ret = Model::$dbo["r"]->findBySql(__class__, "select * from `".self::getTableName()."` where chapter_id='{$chapter_id}' and language_id='{$lan_id}'"); 
		return isset($ret[0]) ? $ret[0] : '';
	}

	public function getResultList($findRelation, $field, $operation, $value, $start, $length)
	{
		$find	=	$findRelation['frontView'];
		$where	=	Model::assembleWhereSentence($find, $field, $operation, $value);
		$sql	=	Model::assembleSqlSentence($findRelation, $where, $start, $length);
		$result	=	Model::$dbo['r']->findBySql(__class__,$sql);

		$sql	=	Model::assembleSqlSentenceForNums($findRelation, $where);
		$retAll	=	Model::$dbo['r']->findBySql(__class__,$sql);

		$list	=	array();
		$list['page']	=	ceil($start / $length) + 1;
		$list['total']	=	ceil($retAll[0]->num / $length);
		$list['records']=	$retAll[0]->num;

		foreach	($result	as	$gridData)
		{
			$tmp			=	array();
			$tmp['id']		=	$gridData->id;
			$tmp['cell']	=	array();

			foreach ($find	as	$name	=>	$column)
			{
				$tmp['cell'][]	=	$gridData->$name;
			}

			$tmp['cell'][]	=	'<a href="get_chapter.html?act=edit&step=2&id='. $gridData->id .'" class="btn edit ui-state-default ui-corner-all"><span class="ui-icon ui-icon-pencil"/>Edit</a><a href="get_chapter.html?act=view&step=2&id='. $gridData->id .'" class="btn view ui-state-default ui-corner-all"><span class="ui-icon ui-icon-close"/>View</a>';
			$list['rows'][]	=	$tmp;
		}

		return	$list;
	}

	/*
	 * by donald
	 * modified at 2011-10-11
	 */
	public static function getAll(array $params)
	{
		foreach ($params as $field => $value) {
			switch ($field) {
				case 'chapter_id':
					$where[] = 'chapter_id = ?';
					$where_array[] = $value;
					break;
			}
		}
	
		return self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
	}

	/*
	 * by donald
	 */
	public function add_change_log($programme_id)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array(
			'chapter_id' => $this->chapter_id,
			'language_id' => $this->language_id,
		);
		$field['is_active'] = 1;
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = 0;
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array(
			'chapter_id' => $this->chapter_id,
			'language_id' => $this->language_id,
		);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

}
?>
